package com.bobo.controller;

import cn.hutool.Hutool;
import cn.hutool.http.HttpUtil;
import com.bobo.domain.Course;
import com.bobo.domain.Student;
import com.bobo.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * @description:
 * @author: BOBO
 * @time: 2022/3/7 15:04
 */

@Controller
public class StudentController {
    @Autowired
    StudentService studentService;
    //去增加
    @RequestMapping("toAdd")
    public String toAdd(Student student){
        return "add";
    }
    @RequestMapping("add")
    public String add(Student student){

        studentService.add(student);
        return "redirect:list";
    }
    @ResponseBody  //返回所有科目
    @RequestMapping("listCourse")
    public List<Course> listCourse(){

        return studentService.listCouse();
    }
    //列表
    @RequestMapping("list")
    public String list(Student student, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3")Integer pageSize, Model model){
        PageInfo<Student> pg = studentService.list(student, pageNum, pageSize);
        model.addAttribute("pg",pg);//查询结果
        model.addAttribute("student",student);//查询条件
        return "list";
    }
    //t跳转到修改
    @RequestMapping("toUpdate")
    public  String toUpdate(Integer id,Model model){
        Student student=  studentService.get(id);
        model.addAttribute("student",student);
        return "update";
    }


    //执行修改
    @RequestMapping("update")
    public String update(Student student){
        studentService.update(student);
        return "redirect:list";
    }

    @RequestMapping("auth")
    public String auth(String code){
        String client_id="d49bc833f5f8123de173f00b787abbd1cf25d32006dd1306581e0a1a7d972cba";
        String secret="86b586c794b56c8adc16ebf1ea516deafad738c6bde959f81d7f317d1fc98774";
        String redirect_uri="http://localhost:8080/auth";
        String url="https://gitee.com/oauth/token?grant_type=authorization_code&code="+
                code+"&client_id="+client_id+"&redirect_uri="+redirect_uri+"&client_secret="+secret;
      /*  HashMap<String, Object> map = new HashMap<>();
        map.put("client_secret",secret);*/
        String post = HttpUtil.post(url, "");
        System.out.println(post+"===========================");



        return "redirect:list";
    }
}

